﻿var NotificationsManager = function(container) {

    var self = this;
    var isLoading = false;
    var currentIndex = 0;
    var initialSetSize = 30;
    var defaultSetSize = 20;
    var visibleElements = 0;
    var notificari = new Array();
    var lastUpdate = null;

    var Elements = {
        notificationsList: container.find('.overlay-notifications div:first'),
        buttonContainer: container.find('.overlay-notifications-button')
    };

    var Templates = {
        notificationTemplate: container.find(".notificationTemplate"),
        notificationTemplateCompiled: null
    };

    this.show = function() {
        container.show();
        if (currentIndex == 0)
            self.getPullUpData();
    };

    this.hide = function() {
        container.hide();
    };

    this.getPullUpData = function(callback) {
        if (isLoading)
            return;
        isLoading = true;

        var setSize = currentIndex == 0 ? initialSetSize : defaultSetSize;

        if (currentIndex == 0)
            lastUpdate = new Date();

        container.find(".loadMore").show();
        app.TMMService.TMM.GetNotifications(currentIndex, setSize, "", function(response) {
            if (response.Exception)
                return;

            addToList(response.FinalResult);
            currentIndex += response.FinalResult.length;

            if (currentIndex > 0)
                Elements.buttonContainer.show();

            if (!visibleElements)
                calculateVisibleElements();

            container.find(".loadMore").hide();
            isLoading = false;
        });
    };

    var calculateVisibleElements = function() {
        visibleElements = Math.ceil(Elements.notificationsList.height() / Elements.notificationsList.find("div:first").outerHeight());
    };

    var addToList = function(data, addToStart) {
        if (!data || !data.length)
            return;

        if (!Templates.notificationTemplateCompiled) {
            var source = Templates.notificationTemplate.html();
            Templates.notificationTemplateCompiled = Handlebars.compile(source);
        }

        var html = "";
        var startIndexInsert = 0;
        var last = notificari.length ? notificari[notificari.length - 1] : null;

        for (var i = 0; i < data.length; i++) {
            var n = data[i];
            n.POZA_SRC = Utils.getEmployeePhoto(n.ArePoza ? n.PRS_ID : 0);
            n.Descriere = "Descriere";

            html += Templates.notificationTemplateCompiled(n);

            if (!addToStart && last && last.CCD_ID == n.CCD_ID) {
                html = "";
                startIndexInsert = i + 1;
            }
        }

        if (startIndexInsert < data.length)
            for (var i = startIndexInsert; i < data.length; i++)
                notificari.push(data[i]);

        if (addToStart) {
            for (var i = data.length - 1; i >= 0; i--)
                notificari.unshift(data[i]);
            Elements.notificationsList.prepend(html);
        } else Elements.notificationsList.append(html);

        Elements.notificationsList.find('.new').each(function() {
            setEditable($(this), isEditing);
            $(this).removeClass('new');
        });

        return data.length - startIndexInsert;
    };

    this.toggle = function() {
        if (container.is(":visible"))
            self.hide();
        else self.show();
    };

    var initialize = function() {
        Elements.notificationsList.css('max-height', ($(window).height() - 200) + 'px');
        app.translateElement(container);

        var error = 10;
        if ($.browser.safari)
            error += 60;

        Elements.notificationsList.find('div:first').scroll(function() {
            var el = $(this);
            var pastBottom = el.scrollTop() + el.height() - marginTop >= el - error;
            if (pastBottom)
                self.getPullUpData();
        });
    };

    initialize();
};